package edu.uestc.canary.admin.web.controller;

import edu.uestc.canary.entity.dto.Result;
import edu.uestc.canary.entity.pojo.Administrator;
import edu.uestc.canary.service.AdministratorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@RestController
@RequestMapping("/administrators")
@Tag(name = "管理员API接口")
@Validated //参数数据校验
//@SessionAttributes(names = "administrator")
public class AdministratorController {
    @Resource private AdministratorService service;
    /**
     * http请求:post
     * @param username 用户名，非空
     * @param password 密码，非空
     * @return 登录成功: Result.ok()
     */
    @PostMapping("/login")
    @Operation(summary = "根据用户名和密码登录")
    @Parameters({@Parameter(name = "username", description = "用户名", required = true),
                 @Parameter(name = "password", description = "密码", required = true)})

    public Result loginByAccount(@NotNull @Size(min = 4, max = 12) String username,
                        @NotNull String password) {
//        if(result.hasErrors()) //校验未通过
//            return Result.fail(null, "用户名和密码不合法...");
        return service.login(username, password);
    }

    /**
     * 通过短信验证码进行登录
     * @param phone 手机号码，需要验证合法性
     * @return 登录成功: Result.ok()
     */
    @GetMapping("/login/{phone}/{code}")
    @Operation(summary = "根据短信验证码进行登录")
    @Parameter(name = "phone", description = "手机号码", required = true, in = ParameterIn.PATH)
    public Result loginByPhone(@PathVariable @Pattern(regexp = "^\\d{5,11}$",message = "非法的手机号码") String phone, @PathVariable String code) {
        return service.loginByPhone(phone,code);
    }

    /**
     * 退出登录
     * @return Result.ok()
     */
    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public Result logout() {
        return Result.ok();
    }

    /**
     * 发送验证码
     * @param phone 手机号码
     * @return
     */
    @GetMapping("/verification-code/{phone}")
    @Operation(summary = "发送短信验证码")
    @Parameter(name = "phone", description = "手机号码", required = true, in = ParameterIn.PATH)
    public Result sendVerificationCode(@PathVariable @Pattern(regexp = "^\\d{5,11}$",message = "非法的手机号码") String phone) {
        return service.sendVerificationCode(phone);
    }

    //public Result getAdministrator(){}
}
